package com.weilan.ark3.controller;


import com.weilan.ark3.sqlite.SQLiteUtils;
import com.weilan.ark3.utils.DateUtils;
import org.springframework.stereotype.Controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//import static com.weilan.ark3.buffer.OverallSituation.SaveTime;

@Controller
public class DeleteOnTime {
    //定时删除任务
    public static void deleteOnTime() throws SQLException {
//        if (SaveTime != 0) {
            //获取表
            List<String> resultSet = SQLiteUtils.selectFromDB();
            //创建要删除的表名的集合
            List<String> deleteTbName = new ArrayList<>();
            String beforeTime = DateUtils.beforeTime(90);
            String time = DateUtils.dateFormatConversion(beforeTime);
            for (int i = 0; i < resultSet.size(); i++) {
                String name = resultSet.get(i);
                if (!name.equals("sqlite_sequence")) {
                    /**
                     * name对应的是tb_contrller_rtu表中的instruct_name字段，要求name名字的字段必需为xx_xx_xxxx,不然会报错
                     * uc_info_20230603  phc_info_20230703
                     */
                    String[] split = name.split("_");
                    if (time.compareTo(split[2]) >= 0) {
                        deleteTbName.add(name);
                    }
                }
            }
            SQLiteUtils.deleteFromDB(deleteTbName);
//        System.out.println("删除数据表:"+deleteTbName);
//        }
    }
}
